﻿using System;
using System.Collections.Generic;
using System.Text;
using HR.Appointments.Service.DisplayLists;

namespace HR.Appointments.Service.FacadeUsers
{
    /// <summary>
    /// Class for regular users of the system.
    /// </summary>
    public class User
    {
        #region Constructors
        /// <summary>
        /// Initializes a new user class
        /// </summary>
        public User()
        {
        }
        #endregion

        #region Get Methods
        /// <summary>
        /// Gets a list of activities for a specific company
        /// </summary>
        /// <param name="companyId">Company identifier</param>
        /// <returns>A list of activities</returns>
        public List<ActivityEntity> GetActivities(int companyId)
        {
            return DAO.Instance.Activity.GetByCompany(companyId);
        }

        /// <summary>
        /// Gets a list of workstation for a specific company
        /// </summary>
        /// <param name="companyId">Company identifier</param>
        /// <returns>A list of workstations</returns>
        public List<WorkstationEntity> GetWorkstations(int companyId)
        {
            return DAO.Instance.Workstation.GetByCompany(companyId);
        }

        /// <summary>
        /// Get a list of orders for a specific phone number
        /// </summary>
        /// <param name="phone">A valid phonenumber</param>
        /// <returns>A display list of orders</returns>
		public List<OrderList> GetPhone(string phone)
		{
			var result = new List<OrderList>();
			var list = DAO.Instance.Order.GetByPhone(phone);
            foreach (OrderEntity obj in list)
			{
				result.Add(new OrderList(obj));
			}
			return result;
		}

        /// <summary>
        /// Get vacant slots for a company by a specific workstation and date that can handle
        /// the choosen activity.
        /// </summary>
        /// <param name="companyId">Company identifier</param>
        /// <param name="date">The date to find vacant slots</param>
        /// <param name="workstationId">Workstation identifier</param>
        /// <param name="activityId">Activity identifier</param>
        /// <returns>A timelist of vacant slot for the date</returns>
        public List<TimeList> GetVacantSlots(int companyId, DateTime date, int workstationId, int activityId)        
        {
            return CalendarBook.Open(companyId, date).GetVacantSlots(workstationId, activityId);
        }

        /// <summary>
        /// Get an instance of a company
        /// </summary>
        /// <param name="companyId">Company identifier</param>
        /// <returns>A company identifier</returns>
        public CompanyEntity GetCompanyInstance(int companyId)
        {
            return DAO.Instance.Company.GetInstance(companyId);
        }

        /// <summary>
        /// Get all open hours for a specific company
        /// </summary>
        /// <param name="companyId">Company identifier</param>
        /// <returns>A list of open hours</returns>
        public List<OpenHoursEntity> GetOpenHours(int companyId)
        {
            return DAO.Instance.OpenHours.GetByCompany(companyId);
        }
        #endregion

        #region Create Methods
        /// <summary>
        /// Inserts a new order to the database and marks it unconfirmed.
        /// </summary>
        /// <param name="order">A new order entity</param>
        public void PlaceOrder(OrderEntity order)
        {
            System.Diagnostics.Debug.Assert(order != null, "Order Entity is null");
            order.Status = OrderStatus.UNCONFIRMED;
            try
            {
                DAO.Instance.Order.Insert(order);
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw new SubmitException("Insert company failed", ex);
            }
        
        }

        /// <summary>
        /// Inserts a new company to the database
        /// </summary>
        /// <param name="company">A new company entity</param>
        public void CreateCompany(CompanyEntity company)
        {
            try
            {
                DAO.Instance.Company.Insert(company);
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw new SubmitException("Insert company failed", ex);
            }
        }
        #endregion
    }
}
